package com.OrderingServer.Controller.seller;


import cn.hutool.core.util.IdUtil;
import com.OrderingServer.pojo.ProductCategory;
import com.OrderingServer.pojo.ProductInfo;
import com.OrderingServer.service.ProductCategoryService;
import com.OrderingServer.service.ProductInfoService;
import com.OrderingServer.service.impl.ProductInfoServiceImpl;
import com.OrderingServer.utils.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingo
 * @since 2025-08-11
 */
@Api(tags = "餐饮信息接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200,message = "新增成功" ,response = ProductCategory.class),
        @ApiResponse(code = 400,message = "参数错误" ),
        @ApiResponse(code = 403,message = "权限被禁止" ),
        @ApiResponse(code = 500,message = "服务器内部错误" ),
})
@RequestMapping("/seller/ProductInfo")
public class ProductInfoController {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    RedisTemplate redisTemplate;
    @ApiOperation("分页查询所有餐饮信息")
    @GetMapping("selectPage")
    public Result getAllByPage(@RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "5")Integer pageSize,
                               ProductInfo productInfo){
        PageInfo<ProductInfo> pageInfo =productInfoService.selectPage(pageNum,pageSize,productInfo);
        return   Result.success(pageInfo);
    }
    @ApiOperation("新增餐饮信息")
    @PostMapping("/insert")
    public Result insert(@RequestBody ProductInfo productInfo){
        //清除缓存
        redisTemplate.delete("productVOList");
        String id= String.valueOf(IdUtil.getSnowflake().nextIdStr());
        productInfo.setProductId(id);
        return new Result().success(productInfoService.save(productInfo));
    }
    @ApiOperation("根据id更新餐饮信息")
    @PutMapping("/update")
    public Result update(@RequestBody ProductInfo productInfo){
        //清除缓存
        redisTemplate.delete("productVOList");
        return new Result().success(productInfoService.updateById(productInfo));
    }
    @ApiOperation("根据id删除餐饮信息")
    @DeleteMapping("delete/{id}")
    public Result delete( @PathVariable("id") String productId){
        //清除缓存
        redisTemplate.delete("productVOList");
        ProductInfo productInfo =new ProductInfo(productId,"1");
        return new Result().success(productInfoService.updateById(productInfo));
    }
}
